package com.me.template4j.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义错误格式
 * @Authhor: qiu
 * @Date: 2022/12/13 1:37 下午
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandle {

    @ExceptionHandler(CustomException.class)
    public ResponseEntity<Map<String,Object>> handleException(CustomException exception) {

        Map<String, Object> map = new HashMap<>();
        map.put("message",exception.getErrMsg());
        map.put("error",exception.getStatus());
        map.put("status",exception.getStatus().value());
        return new ResponseEntity<>(map,exception.getStatus());
    }


}
